PROGRAM CCSD
!
! INCORE PROGRAM FOR CCSD CALCULATIONS 
!
use mod_iop
use mod_size
use mod_orbit
use mod_ioff 
implicit none
integer nvn, non, i0
integer ie, iend, ifij, ifab
integer ifia, it1, it2, iijkl, iijka 
integer it12, it22, ir12, ir22, iend2
integer iiabj, iabij, iabci, iabcd, ir1, ir2
integer ifmi, ifae, ifme
integer iwmnij, iwmbej
integer maxcore, nneed, icrsizg
integer, external :: irpdso, idsymsoc
real*8 tt1, tt2, rt1, rt2
real*8 escf, eccsd, eccsdt
real*4, allocatable :: icore(:) 
real*4,device, allocatable :: icoreg(:) 
!
call cpu_time(tt1); call walltimeso(rt1)
call readinp
call read_mod
write(6,*)'totgpu', totgpu
!icrsiz = 1024*1024*1024/8*80 ! 110GB
!icrsizg = 1024*1024*1024/8*10 ! 11GB
allocate(icore(icrsiz))
!
! various lengths
!
nvn = nva + nvb
non = noa + nob
!
ie=1
iend=ie+(nvn+non)*isd
!
!memory fo t1, t2 and r1, r2
write(6,*)'nioi3', nioi3, isd
it1 = iend
it2 = it1 + nifvo*isd
ir1 = it2 + nioi3*isd
ir2 = ir1 + nifvo*isd
iend = ir2 +nioi3*isd
!
! memory for fock matrices
!
ifij=iend
ifab=ifij +nifoo*isd
ifia=ifab +nifvv*isd
iend =ifia+nifvo*isd
!
! memory for fae, fmi and fme 
!
ifmi=iend
ifae=ifmi +nifoo*isd
ifme=ifae +nifvv*isd
iend =ifme+nifvo*isd
!
! memory for small two-electron integrals
!
write(6,*)'test for small'
write(6,'(A, I14, I14, I14, I14)')'nioi', nioi1, nioi2, nioi3, nioi4
iijkl = iend 
iwmnij = iijkl +nioi1*isd
iijka = iwmnij +nioi1*isd
iabij = iijka  +nioi2*isd
iend  = iabij  +nioi3*isd
nneed = iend + nioiri_vv*isd + max(nvvoo*6,nvvv*2+nvvoo*2)*isd
if(nneed > icrsiz) then 
   write(6,*)"memory not enough for DF-CCSD"
   stop
endif

iiabj = iend 
iwmbej = iiabj +nioi4*isd  ! iiabj(bbaa)  = iiabj(aabb)
iend = iwmbej  +nioi4*isd   ! wmbej(aabb) /= wmbej(bbaa)
nneed = iend + nioiri_vv*isd + max(nvvoo*6,nvvv*2+nvvoo*2)*isd
write(6,*)'size of iijkl          ', nioi1/1024.d0/1024.d0*4*isd, 'MB'
write(6,*)'size of iijka          ', nioi2/1024.d0/1024.d0*4*isd, 'MB'
write(6,*)'size of t2 and r2      ', nioi3/1024.d0/1024.d0*4*isd, 'MB'
write(6,*)'size of wmbej and iiabj', nioi4/1024.d0/1024.d0*4*isd, 'MB'
write(6,*)'size of iabci          ', nioi5/1024.d0/1024.d0*4*isd, 'MB'
write(6,*)'size of iabcd          ', nioi6/1024.d0/1024.d0*4*isd, 'MB'
!
write(6,*)'memroy needed for ri_calc', nneed/1024.d0/1024.d0*4, 'MB'
iowmbej = 0
if(nneed>icrsiz) then 
   iowmbej = 1
  iend = iend - nioi4*2*isd 
endif
!
! memory for large two-electron integerals
!
igpuall=0
if(nneed<igpusize.and.totgpu==1) then
   write(6,*)'ri can be calculated all in GPU  '
   igpuall=1
endif
!
maxcore = icrsiz - iend
 call run(icore(ie),                                           &   
          icore(it1), icore(it2), icore(ir1), icore(ir2),      &
          icore(ifij),icore(ifab),icore(ifia),                 &
          icore(ifae), icore(ifmi), icore(ifme),               &
          icore(iijkl), icore(iwmnij), icore(iijka),           &
          icore(iabij), icore(iiabj), icore(iwmbej),           &
          icore(iend),             &
          maxcore, escf, eccsd, eccsdt)                    
call put_t(icore(it1), icore(it2), icore(iend))
call cpu_time(tt2); call walltimeso(rt2)
write(6,*)'cpu and real time for ccsd', tt2-tt1, rt2-rt1
!
end



















